<?php

/**
 * 产品类
 */
class House
{
	private $_wall;
	private $_door;
	private $_window;
	private $_ceiling;
	private $_floor;
	
	public function setWall($wall)
	{
		$this->_wall = $wall;
	}
	
	public function getWall()
	{
		return $this->_wall;
	}
	
	public function setDoor($door)
	{
		$this->_door = $door;
	}
	
	public function getDoor()
	{
		return $this->_door;
	}
	
	public function setWindow($window)
	{
		$this->_window = $window;
	}
	
	public function getWindow()
	{
		return $this->_window;
	}
	
	public function setCeiling($ceiling)
	{
		$this->_ceiling = $ceiling;
	}
	
	public function getCeiling()
	{
		return $this->_ceiling;
	}
	
	public function setFloor($floor)
	{
		$this->_floor = $floor;
	}
	
	public function getFloor()
	{
		return $this->_floor;
	}
}

/**
 * 抽象建造者
 */
abstract class Builder
{
	public $house;
	
	public function __construct()
	{
		$this->house = new House();
	}
	
	/**
	 * 返回产品对象
	 */
	public function getResult()
	{
		return $this->house;
	}
	
	public abstract function buildWall();
	public abstract function buildDoor();
	public abstract function buildWindow();
	public abstract function buildCeiling();
	public abstract function buildFloor();
	
	/**
	 * 钩子方法：是否需要地板
	 */
	public function isFloor()
	{
		return true;
	}
}

/**
 * 具体建造者
 */
class ChineseBuilder extends Builder
{
	public function buildWall()
	{
		$this->house->setWall('中式风格的墙');
	}
	
	public function buildDoor()
	{
		$this->house->setDoor('中式风格的门');
	}
	
	public function buildWindow()
	{
		$this->house->setWindow('中式风格的窗户');
	}
	
	public function buildCeiling()
	{
		$this->house->setCeiling('中式风格的天花板');
	}
	
	public function buildFloor()
	{
		$this->house->setFloor('中式风格的地板');
	}
	
	public function isFloor()
	{
		return false;
	}
}

/**
 * 具体建造者
 */
class WesternBuilder extends Builder
{
	public function buildWall()
	{
		$this->house->setWall('西式风格的墙');
	}
	
	public function buildDoor()
	{
		$this->house->setDoor('西式风格的门');
	}
	
	public function buildWindow()
	{
		$this->house->setWindow('西式风格的窗户');
	}
	
	public function buildCeiling()
	{
		$this->house->setCeiling('西式风格的天花板');
	}
	
	public function buildFloor()
	{
		$this->house->setFloor('西式风格的地板');
	}
}

/**
 * 导演类
 */
class Director
{
	private $builder;
	
	public function __construct($builder)
	{
		$this->builder = $builder;
	}
	
	public function getHouse()
	{
		//组合到一起的算法（相对稳定）
		$this->builder->buildWall();
		$this->builder->buildDoor();
		$this->builder->buildWindow();
		$this->builder->buildCeiling();
		if ($this->builder->isFloor())
			$this->builder->buildFloor();
		return $this->builder->getResult();
	}
}

class Client
{
	public static function main()
	{
		$builder = new ChineseBuilder();
		//$builder = new WesternBuilder();
		$director = new Director($builder);
		$house = $director->getHouse();
		var_dump($house);
	}
}

Client::main();